class Solution {
    public String decodeString(String s) {
        Stack<StringBuffer> a= new Stack<>();
        Stack<Integer> b= new Stack<>();
        char[] ss = s.toCharArray();
        int n=s.length();
        a.push(new StringBuffer());
        int i=0;
        while(i<n){
            if(ss[i]>='0'&&ss[i]<='9'){
                int tmp=0;
                while(ss[i]>='0'&&ss[i]<='9'){
                    tmp=tmp*10+(ss[i]-'0');
                    i++;
                }
                b.push(tmp);
            }else if(ss[i]=='['){
                i++;
                StringBuffer tmp=new StringBuffer();
                while(i<n&&ss[i]>='a'&&ss[i]<='z'){
                    tmp.append(ss[i]);
                    i++;
                }
                a.push(tmp);
            }else if(ss[i]==']'){
                int k=b.pop();
                StringBuffer str=a.pop();
                while(k--!=0){
                    a.peek().append(str);
                }
                i++;
            }else{
                StringBuffer tmp=new StringBuffer();
                while(i<n&&ss[i]>='a'&&ss[i]<='z'){
                    tmp.append(ss[i]);
                    i++;
                }
                a.peek().append(tmp);
            }
        }
        return a.peek().toString();
    }
}